package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.model.media.dtos.WmMaterialDto;
import com.heima.wemedia.entity.WmMaterial;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.wemedia.service.IWmMaterialService;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * <p>
 * 自媒体图文素材信息表 前端控制器
 * </p>
 *
 * @author heima
 * @since 2021-03-15
 */
@RestController
@RequestMapping("/api/v1/material")
@Api(tags = "自媒体图文素材信息表接口")
@CrossOrigin
public class WmMaterialController{

    @Autowired
    private IWmMaterialService wmMaterialService;

    /**
     * 素材上传以及保存素材功能
     */
    @PostMapping("/upload_picture")
    public ResponseResult<String> uploadPicture(@RequestParam("multipartFile") MultipartFile multipartFile){
        String imageUrl = wmMaterialService.uploadPicture(multipartFile);
        return ResponseResult.ok(imageUrl);
    }

    /**
     * 分页查询素材数据
     */
    @PostMapping("/list")
    public PageResult<WmMaterial> findByPage(@RequestBody  @Valid WmMaterialDto dto){
        return wmMaterialService.findByPage(dto);
    }

    /**
     * 素材收藏
     */
    @PutMapping("/collect/{id}")
    public ResponseResult collect(@PathVariable("id") Integer id){
        wmMaterialService.updateIsCollection(id,true);
        return ResponseResult.ok();
    }

    /**
     * 取消素材收藏
     */
    @PutMapping("/cancel_collect/{id}")
    public ResponseResult cancelCollect(@PathVariable("id") Integer id){
        wmMaterialService.updateIsCollection(id,false);
        return ResponseResult.ok();
    }

    /**
     * 删除素材
     */
    @DeleteMapping("/del_picture/{id}")
    public ResponseResult deletePic(@PathVariable("id") Integer id){
        wmMaterialService.deletePic(id);
        return ResponseResult.ok();
    }
}
